package Test;

import java.util.PriorityQueue;

/**
 * @author Kevin
 * @date 2020/3/1 12:56
 */
public class TopKCount {
    public int[] tpkK(int[] data, int k) {
        //是二叉树小顶堆
        PriorityQueue<Integer> queue = new PriorityQueue<>();

        for (int i = 0; i < data.length; i++) {
            if (queue.size() < k) {
                queue.offer(data[i]);
            } else {
                int val = queue.peek();
                if (data[i] > val) {
                    queue.poll();
                    queue.offer(data[i]);
                }
            }
        }

        int[] result = new int[k];
        int index = 0;
        while (!queue.isEmpty()) {
            result[index++] = queue.poll();
        }
        return result;
    }
}
